# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        Young  
# Name:         序列与迭代
# Description:
# Author:       asus
# Date:         2019/3/5
#-------------------------------------------------------------------------------


#1.获取字典中key的列表
dict = {'x':1,'y':2,'z':3}
#输出x y z
for key in dict:
    print(key,end=' ')

print()


#2.使用items方法同时获取字典中的key和value表
dict = {'x':1,'y':2,'z':3}
#同时获取key和value
for key,value in dict.items():
    print(key,value,end=' ')

print()


#3.并行迭代range函数
names = ["Bill","Mary","John"]
ages = [30,40,20]
#range函数
for i in range(len(names)):
    print(names[i],ages[i],end=" ")

print()


#4.压缩序列zip函数
companies = ["欧瑞科技","Google","Facebook"]
websites = ["http://geekori.com","http:google.com"]
for value in zip(companies,websites):
    print(value,end= " ")

print()


#5.反转序列迭代reversed函数
companies = reversed(["欧瑞科技","Google","Facebook"])
for value in companies:
    print(value,end=" ")

print()


#例题
#定义一个字典
dict = {"name":"Bill","age":34,"sex":"男","salary":"3456"}
for key in dict:
    print(key,"=",dict[key],end = " ")

print()

#迭代字典的key和value
for key,value in dict.items():
    print(key,"=",value,end=" ")

print()

#并行迭代
list1 = [1,2,3,4,5]
list2 = ["a","b","c","d","e"]
#同时迭代list1和list2
for i in range(len(list1)):
    print(list1[i],list2[i],end =" ")

print()

#压缩迭代
for value in zip(list1,list2):
    print(value,end = " ")
print()
list3 = ['x','y']
for value in zip(list2,list3):
    print(value,end = " ")
print()

#反转排序迭代
values1 = [4,1,3,6,5,2,8]
#对values排序
print(sorted(values1))
#对列表进行反转
values2 = reversed(values1)
for v in values2:
    print(v,end=" ")
print()
#反抓hello world
print(''.join(list(reversed("hello world"))))
